package com.zzyl.nursing.service.impl;

import java.util.List;

import com.zzyl.nursing.domain.NursingProjectPlan;
import com.zzyl.nursing.domain.dto.NursingPlanDTO;
import com.zzyl.nursing.domain.vo.NursingPlanVO;
import com.zzyl.nursing.domain.vo.NursingPlansVO;
import com.zzyl.nursing.mapper.NursingProjectPlanMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.NursingPlanMapper;
import com.zzyl.nursing.domain.NursingPlan;
import com.zzyl.nursing.service.INursingPlanService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;

/**
 * 护理计划Service业务层处理
 *
 * @author ruoyi
 * @date 2025-09-12
 */
@Service
public class NursingPlanServiceImpl extends ServiceImpl<NursingPlanMapper, NursingPlan> implements INursingPlanService
{
    @Autowired
    private NursingPlanMapper nursingPlanMapper;

    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;

    /**
     * 查询护理计划
     *
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Override
    public NursingPlan selectNursingPlanById(Long id)
    {
        return getById(id);
    }

    /**
     * 查询护理计划列表
     *
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan)
    {
        return nursingPlanMapper.selectNursingPlanList(nursingPlan);
    }

    /**
     * 新增护理计划
     *
     * @param dto 护理计划
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertNursingPlan(NursingPlanDTO dto)
    {
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(dto, nursingPlan);
        this.save(nursingPlan);

        if(nursingPlan.getProjectPlans()!=null){
            for (NursingProjectPlan projectPlan : nursingPlan.getProjectPlans()) {
                projectPlan.setPlanId(nursingPlan.getId());
            }
            nursingProjectPlanMapper.insertBatch(nursingPlan.getProjectPlans());
        }



        return 1;
    }

    /**
     * 修改护理计划
     *
     * @param nursingPlan 护理计划
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateNursingPlan(NursingPlanDTO nursingPlan)
    {
        NursingPlan plan  = new NursingPlan();
        BeanUtils.copyProperties(nursingPlan, plan);
        updateById(plan);

        if(nursingPlan.getProjectPlans()!=null){
            nursingProjectPlanMapper.deleteById1(plan.getId());
            nursingPlan.getProjectPlans().forEach(projectPlan -> {
                projectPlan.setPlanId(plan.getId());
            });
            nursingProjectPlanMapper.insertBatch(nursingPlan.getProjectPlans());
        }
        return 1;
    }

    /**
     * 批量删除护理计划
     *
     * @param id 需要删除的护理计划主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteNursingPlanByIds(Long id)
    {
         removeById(id);
         nursingProjectPlanMapper.deleteById1(id);
         return 1;
    }

    /**
     * 删除护理计划信息
     *
     * @param id 护理计划主键
     * @return 结果
     */
    @Override
    @Transactional( rollbackFor = Exception.class)
    public int deleteById(Long id)
    {
        removeById(id);
        nursingProjectPlanMapper.deleteById1(id);
        return 1;
    }

    @Override
    public NursingPlanVO getNursingPlanById(Long id) {
        NursingPlanVO nursingPlanVO =nursingPlanMapper.getPlanById(id);
        nursingPlanVO.setProjectPlans(nursingProjectPlanMapper.getNursingProjectPlanById(id));

        return nursingPlanVO;
    }

    @Override
    public List<NursingPlansVO> getall() {
        List<NursingPlansVO> list = nursingPlanMapper.getall();
        return list;
    }
}